package org.jeecg.modules.compilation.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.compilation.dto.FavouriteSearchDTO;
import org.jeecg.modules.compilation.entity.CiDoc;
import org.jeecg.modules.compilation.entity.CiFavorite;
import org.jeecg.modules.compilation.service.ICiFavoriteService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: ci_favorite
 * @Author: jeecg-boot
 * @Date:   2024-03-15
 * @Version: V1.0
 */
@Api(tags="ci_favorite")
@RestController
@RequestMapping("/compilation/ciFavorite")
@Slf4j
public class CiFavoriteController extends JeecgController<CiFavorite, ICiFavoriteService> {
	@Autowired
	private ICiFavoriteService ciFavoriteService;


	 /**
	  * 获取收藏夹数据
	  * @return
	  */
	@AutoLog(value = "获取收藏夹数据")
	@ApiOperation(value="获取收藏夹数据", notes="获取收藏夹数据")
	@GetMapping("/getFavoriteList")
	public Result<List<CiFavorite>> getFavoriteList(){
		return Result.OK(ciFavoriteService.getFavoriteList());
	}

	 /**
	  * 获取文件数据
	  * @param documentSn
	  * @return
	  */
	 @AutoLog(value = "获取文件数据")
	 @ApiOperation(value="获取文件数据", notes="获取文件数据")
	 @GetMapping("/getDoc")
	 public Result<CiDoc> getDoc(@RequestParam String documentSn){
		 return Result.OK(ciFavoriteService.getDoc(documentSn));
	 }
	 /**
	  * 标题搜素
	  */
	 @ApiOperation(value = "收藏夹document标题搜素", notes = "标题搜素")
	 @PostMapping("/search")
	 public Result<List<CiFavorite>>  qureyList(@RequestBody FavouriteSearchDTO favouriteSearchDTO) throws IOException {
		 return Result.OK(ciFavoriteService.queryList(favouriteSearchDTO));
	 }
	 /**
	  *  更改收藏状态
	  *
	  * @param ciFavorite
	  * @return
	  */
	 @AutoLog(value = "ci_favorite-更改收藏状态")
	 @ApiOperation(value="ci_favorite-更改收藏状态", notes="ci_favorite-更改收藏状态")
//	@RequiresPermissions("compilation:ci_favorite:add")
	 @PostMapping(value = "/updateFavorite")
	 public Result<String> updateFavorite(@RequestBody CiFavorite ciFavorite) {
		 return Result.OK(ciFavoriteService.updateFavorite(ciFavorite));
	 }

	 /*
	 取消收藏
	  */
//	 @AutoLog(value = "ci_favorite-通过docSn取消收藏")
//	 @ApiOperation(value="ci_favorite-通过docSn取消收藏", notes="ci_favorite-通过docSn取消收藏")
////	@RequiresPermissions("compilation:ci_favorite:delete")
//	 @DeleteMapping(value = "/omit")
//	 public Result<String> omit(@RequestParam(name="document_sn",required=true) String docSn) {
//		 ciFavoriteService.omit(docSn);
//		 return Result.OK("取消收藏成功!");
//	 }





















	 /**
	  * 分页列表查询
	  *
	  * @param ciFavorite
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 //@AutoLog(value = "ci_favorite-分页列表查询")
	 @ApiOperation(value="ci_favorite-分页列表查询", notes="ci_favorite-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<IPage<CiFavorite>> queryPageList(CiFavorite ciFavorite,
													@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													HttpServletRequest req) {
		 QueryWrapper<CiFavorite> queryWrapper = QueryGenerator.initQueryWrapper(ciFavorite, req.getParameterMap());
		 Page<CiFavorite> page = new Page<CiFavorite>(pageNo, pageSize);
		 IPage<CiFavorite> pageList = ciFavoriteService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 /**
	  *   添加
	  *
	  * @param ciFavorite
	  * @return
	  */
	 @AutoLog(value = "ci_favorite-添加")
	 @ApiOperation(value="ci_favorite-添加", notes="ci_favorite-添加")
//	@RequiresPermissions("compilation:ci_favorite:add")
	 @PostMapping(value = "/add")
	 public Result<String> add(@RequestBody CiFavorite ciFavorite) throws IOException {
		 ciFavoriteService.save(ciFavorite);
		 ciFavoriteService.insertById(Long.valueOf(ciFavorite.getId()));
		 return Result.OK("添加成功！");
	 }

	 /**
	  *  编辑
	  *
	  * @param ciFavorite
	  * @return
	  */
	 @AutoLog(value = "ci_favorite-编辑")
	 @ApiOperation(value="ci_favorite-编辑", notes="ci_favorite-编辑")
//	@RequiresPermissions("compilation:ci_favorite:edit")
	 @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> edit(@RequestBody CiFavorite ciFavorite) throws IOException {
		 ciFavoriteService.updateById(ciFavorite);
		 ciFavoriteService.insertById(Long.valueOf(ciFavorite.getId()));
		 return Result.OK("编辑成功!");
	 }

	 /**
	  *   通过id删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "ci_favorite-通过id删除")
	 @ApiOperation(value="ci_favorite-通过id删除", notes="ci_favorite-通过id删除")
//	@RequiresPermissions("compilation:ci_favorite:delete")
	 @DeleteMapping(value = "/delete")
	 public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		 ciFavoriteService.removeById(id);
		 ciFavoriteService.deleteById(Long.valueOf(id));
		 return Result.OK("删除成功!");
	 }

	 /**
	  *  批量删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "ci_favorite-批量删除")
	 @ApiOperation(value="ci_favorite-批量删除", notes="ci_favorite-批量删除")
//	@RequiresPermissions("compilation:ci_favorite:deleteBatch")
	 @DeleteMapping(value = "/deleteBatch")
	 public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		 this.ciFavoriteService.removeByIds(Arrays.asList(ids.split(",")));
		 this.ciFavoriteService.deleteBatch(Arrays.asList(ids.split(",")));
		 return Result.OK("批量删除成功!");
	 }

	 /**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "ci_favorite-通过id查询")
	@ApiOperation(value="ci_favorite-通过id查询", notes="ci_favorite-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CiFavorite> queryById(@RequestParam(name="id",required=true) String id) {
		CiFavorite ciFavorite = ciFavoriteService.getById(id);
		if(ciFavorite==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(ciFavorite);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param ciFavorite
    */
//    @RequiresPermissions("compilation:ci_favorite:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, CiFavorite ciFavorite) {
        return super.exportXls(request, ciFavorite, CiFavorite.class, "ci_favorite");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
//    @RequiresPermissions("compilation:ci_favorite:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, CiFavorite.class);
    }

}
